package com.ddm.service.impl.vxImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.entity.vxEntity.Announcement;
import com.ddm.entity.vxEntity.Exam;
import com.ddm.enumeration.AnnouncementType;
import com.ddm.mapper.AnnouncementMapper;
import com.ddm.result.PageResult;
import com.ddm.service.vxService.AnnouncementService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.AnnouncementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Autowired
    AnnouncementMapper announcementMapper;
    @Override
    public PageResult getAnnouncementList(PageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<Announcement> wrapper = new LambdaQueryWrapper<>();
        Page<Announcement> page = new Page<>(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        wrapper.orderByAsc(Announcement::getId);
        announcementMapper.selectPage(page, wrapper);
        long total = page.getTotal();
        List<Announcement> records = page.getRecords();
        List<AnnouncementVO> announcementVOs = convertToVOList(records);
        PageResult pageResult = new PageResult(total,announcementVOs);
        return pageResult;
    }

    @Override
    public PageResult getAnnouncementListByType(PageQueryDTO pageQueryDTO, AnnouncementType type) {
        LambdaQueryWrapper<Announcement> wrapper = new LambdaQueryWrapper<>();
        Page<Announcement> page = new Page<>(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        wrapper.eq(Announcement::getType, type);
        wrapper.orderByAsc(Announcement::getId);
        announcementMapper.selectPage(page, wrapper);
        long total = page.getTotal();
        List<Announcement> records = page.getRecords();
        List<AnnouncementVO> announcementVOs = convertToVOList(records);
        PageResult pageResult = new PageResult(total,announcementVOs);
        return pageResult;
    }

    private List<AnnouncementVO> convertToVOList(List<Announcement> announcements) {
        List<AnnouncementVO> vos = new ArrayList<>();
        for (Announcement announcement : announcements) {
            AnnouncementVO vo = new AnnouncementVO();
            vo.setId(announcement.getId());
            vo.setName(announcement.getName());
            vo.setBeginTime(announcement.getBeginTime());
            vo.setEndTime(announcement.getEndTime());
            vo.setContent(announcement.getContent());
            vo.setImage(announcement.getImage());
            vo.setType(announcement.getType() != null ? announcement.getType().getValue() : null);
            vos.add(vo);
        }
        return vos;
    }
}
